package com.xca.znb.view;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xca.znb.R;
import com.xca.znb.contact.UpdatePasswordContact;
import com.xca.znb.model.impl.UpdatePasswordImpl;
import com.xca.znb.presenter.UpdatePasswordPresenter;

public class UpdateLoginPassword extends AppCompatActivity implements UpdatePasswordContact {
    private ImageButton loginPawBack ;
    private UpdatePasswordPresenter presenter ;
    private EditText oldPaw , newPaw , newPawAgain;
    private Button Submit ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_login_password);
        loginPawBack = findViewById(R.id.loginPaw_back_btn);
        oldPaw = findViewById(R.id.old_login_password);
        newPaw = findViewById(R.id.new_login_password);
        newPawAgain = findViewById(R.id.new_login_password_again);
        Submit = findViewById(R.id.submit_login_password) ;

        UpdatePasswordImpl updatePassword = new UpdatePasswordImpl(this);
        presenter = new UpdatePasswordPresenter(this,updatePassword);

        Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String oldPsw = oldPaw.getText().toString();
                String newPsw = newPaw.getText().toString();
                String newAgain = newPawAgain.getText().toString();

                if (TextUtils.isEmpty(oldPsw)||TextUtils.isEmpty(newPsw)||TextUtils.isEmpty(newAgain)){
                    Toast.makeText(UpdateLoginPassword.this, "所有字段都必须填写", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (oldPsw.isEmpty()){
                    Toast.makeText(UpdateLoginPassword.this, "请输入原密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (newPsw.isEmpty()){
                    Toast.makeText(UpdateLoginPassword.this, "新密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (newAgain.isEmpty()){
                    Toast.makeText(UpdateLoginPassword.this, "确认密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (oldPsw.equals(newPsw)){
                    Toast.makeText(UpdateLoginPassword.this, "新旧密码不能一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!newPsw.equals(newAgain)){
                    Toast.makeText(UpdateLoginPassword.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
                String phone = sharedPreferences.getString("phone" , "");
                presenter.updatePassword(phone ,oldPsw , newPsw);
                boolean isUpdate = presenter.updateLoginSuccessful(phone,newPsw);
                if (isUpdate){
                    finish();
                    Toast.makeText(UpdateLoginPassword.this, "密码修改成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(UpdateLoginPassword.this, "原密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });

        loginPawBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }


    @Override
    public void defaultSetPassword() {

    }

    @Override
    public void oldPasswordWrong() {
        Toast.makeText(this, "原密码错误", Toast.LENGTH_SHORT).show();
    }
}
